package myDaos;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.entities.StockUpdateStatus;

@Stateless
public class StockUpdatedDao {

	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	public List<String> getCountriesList(String region){
		List<String> clist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT DISTINCT s.country FROM StockUpdateStatus s WHERE 1=1 "); 
		    	
			if(region!=null && !region.trim().equals(""))
    	    {
				queryBiffer.append(" and s.region = :region") ;	
    	    }
			Query q = getEntityManager().createQuery(queryBiffer.toString());
			if(region!=null && !region.trim().equals("")){q.setParameter("region", region);} 
			    clist = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clist;
	}
	
	public List<StockUpdateStatus> getStockUpadteList(String country,String region)
	{
		List<StockUpdateStatus> slist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append("SELECT s FROM StockUpdateStatus s WHERE 1=1"); 	
			
			if(country!=null && !country.trim().equals(""))
    	    {
				queryBiffer.append(" AND s.country = :country") ;	
    	    }
			
			if(region!=null && !region.trim().equals(""))
    	    {
				queryBiffer.append("  AND s.region = :region") ;	
    	    }
				
			
			System.out.println(queryBiffer.toString());
			
			Query q = getEntityManager().createQuery(queryBiffer.toString());

			if(country!=null && !country.trim().equals("")){q.setParameter("country", country);}
			if(region!=null && !region.trim().equals("")){q.setParameter("region", region);}
			
			    slist = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return slist;
	}

}
